package me.zhengjie.pm.projecttask.domain;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.Data;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import me.zhengjie.pm.projectbase.domain.ProjectBase;
import me.zhengjie.pm.projectstage.domain.ProjectStage;
import org.hibernate.annotations.ColumnTransformer;

import javax.persistence.*;
import javax.validation.constraints.*;
import java.sql.Timestamp;
import java.io.Serializable;

/**
* @author yuxj
* @date 2020-04-26
*/
@Entity
@Data
@Table(name="project_task")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class ProjectTask implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    /** 项目 */
    @Column(name = "project_id")
    private Integer projectId;

    @ManyToOne
    @JoinColumn(name = "project_id",insertable = false, updatable = false, nullable=true)
    private ProjectBase projectBase;

    /** 项目阶段 */
    @Column(name = "stage_id")
    private Integer stageId;

    @ManyToOne
    @JoinColumn(name = "stage_id",insertable = false, updatable = false, nullable=true)
    private ProjectStage projectStage;
    /** 分析文件 */
    @Column(name = "source_file")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(source_file), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String sourceFile;

    /** 来源章节和描述信息 */
    @Column(name = "source_section")
//    @ColumnTransformer(
//            read = "CAST(AES_DECRYPT(UNHEX(source_section), 'rjb') as char(128))",
//            write = "HEX(AES_ENCRYPT(?, 'rjb'))"
//    )
    private String sourceSection;

    /** 计划开始时间 */
    @Column(name = "plan_start_time")
    private Timestamp planStartTime;

    /** 计划完成时间 */
    @Column(name = "plan_finish_time")
    private Timestamp planFinishTime;

    /** 预计工作量 */
    @Column(name = "workload")
    private String workload;

    /** 开发难度权重 */
    @Column(name = "weight")
    private Double weight;

    /** 任务在项目阶段占比 */
    @Column(name = "ratio")
    private Double ratio;

    /** 工作完成状态 */
    @Column(name = "work_state")
    private String workState;

    public void copy(ProjectTask source){
        BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
    }
}